package teaching.io.temp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import teaching.io.entity.Student;

/**
 * 数据文件存取对象信息示例。
 *
 * @author deng.zhao
 *
 */
public class DataFileExample {

    /**
     * 将给定的学员列表写入数据文件。
     *
     * @param students
     *            学员列表；
     */
    public static void writeStudents(List<Student> students) {
        try (DataOutputStream dos = new DataOutputStream(
                new FileOutputStream("d:/stu.dat", true));) {
            for (final Student student : students) {
                writeStudent(dos, student);
            }
        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用给定输出流，将学员输出。
     *
     * @param dos
     *            数据输出流；
     * @param student
     *            学员；
     * @throws IOException
     *             IO异常；
     */
    public static void writeStudent(DataOutputStream dos, Student student) throws IOException {
        dos.writeUTF(student.getId());
        dos.writeUTF(student.getName());
        final GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(student.getBirthDate());
        dos.writeInt(calendar.get(Calendar.YEAR));
        dos.writeInt(calendar.get(Calendar.MONTH));
        dos.writeInt(calendar.get(Calendar.DATE));
        dos.writeChar(student.getGender());
    }

    public static List<Student> readStudents() {
        final List<Student> students = new ArrayList<Student>();

        try (DataInputStream dis = new DataInputStream(new FileInputStream("d:/stu.dat"));) {
            while (dis.available() > 0) {
                final Student stu = readStudent(dis);
                students.add(stu);
            }
        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }

        return students;
    }

    public static Student readStudent(DataInputStream dis) throws IOException {
        final String id = dis.readUTF();
        final String name = dis.readUTF();
        final GregorianCalendar calender = new GregorianCalendar(dis.readInt(), dis.readInt(),
                dis.readInt());
        final Character gender = dis.readChar();
        final Student stu = new Student(id, name, calender.getTime(), gender);
        return stu;
    }

    public static void main(String[] args) {
        // 创建学生对象列表；
        final List<Student> students = new ArrayList<Student>();
        students.add(new Student("0001", "张三",
                new GregorianCalendar(1997, Calendar.JANUARY, 28).getTime(), 'M'));
        students.add(new Student("0002", "李四",
                new GregorianCalendar(1996, Calendar.DECEMBER, 7).getTime(), 'F'));
        students.add(new Student("0003", "王五",
                new GregorianCalendar(1997, Calendar.DECEMBER, 20).getTime(), 'F'));

        // 写入测试；
        writeStudents(students);

        // 读出测试；
        for (final Student student : readStudents()) {
            System.out.println(student);
        }
    }

}
